package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.binding.MapperMethod;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @author chm
 * @version 1.0.0 v
 * @date 2024-01-29 9:54
 */
/*
这是一个自定义切面类，加入自定义注解
 */
   //自动填充
   @Aspect
   //加入组件
    @Component
   //记录日志
   @Slf4j
public class AutoFillAspect {
//指定切入点
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public  void  autoFillPointCut(){}

    //前置通知,进行自动赋值
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint)  {
        log.info("进行对公共字段填充");

        //方法签名对象
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        //获得注解对象
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        //获得操作类型
        OperationType operationType = autoFill.value();

        //获取拦截数据的参数
        Object[] args = joinPoint.getArgs();
        //判断是否有参数
        if (args == null || args.length == 0){
            return;
        }
        //如果不返回，直接拿数据
       Object entity= args[0];
        
        //准备数据
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();
        //赋值（反射）
        //判断是否为输入
        if (operationType==OperationType.INSERT){
            //获得set方法
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                //利用反射进行赋值
                setCreateTime.invoke(entity,now);
                setCreateUser.invoke(entity,currentId);
                setUpdateTime.invoke(entity,now);
                setUpdateUser.invoke(entity,currentId);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        else if (operationType==OperationType.UPDATE){}
        //是否为修改
        try {

            Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
            Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

            //利用反射进行赋值

            setUpdateTime.invoke(entity,now);
            setUpdateUser.invoke(entity,currentId);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
